<?php
/**
 * Created by PhpStorm.
 * User: Thans
 * Date: 2018/10/17
 * Time: 14:59
 */

namespace app\common\controller;


use think\Exception;
use think\facade\Request;

class AddonsBase extends Base
{
    protected $d_id;
    protected $addons;
    protected $_addons;
    protected $_controller;
    protected $_action;
    protected $options;

    public function __construct()
    {
        $request = Request::instance();
        $routeInfo = $request->routeInfo();
        if (isset($routeInfo['var']['_addons'])) {
            $this->_addons = $routeInfo['var']['_addons'];
            if (isset($routeInfo['var']['_controller'])) {
                $this->_controller = $routeInfo['var']['_controller'];
            }
            if (isset($routeInfo['var']['_action'])) {
                $this->_action = $routeInfo['var']['_action'];
            }
        } else {
            $route = $routeInfo['route'];
            $route = explode('\\', $route);
            if (!isset($route[1])) {
                throw new Exception('系统错误,插件解析失败');
            }
            $this->_addons = $route[1];
            $res = preg_match('/(\S*)Controller/', $route[3], $matches);
            if ($res) {
                $this->_controller = $matches[1];
                if (!isset($matches[1])) {
                    throw new Exception('系统错误,插件解析失败');
                }
            } else {
                throw new Exception('系统错误,插件解析失败');
            }
            $res = preg_match('/@(\S*)$/', parse_url($route[3])['path'], $matches);
            if ($res) {
                $this->_action = $matches[1];
                if (!isset($matches[1])) {
                    throw new Exception('系统错误,插件解析失败');
                }
            } else {
                throw new Exception('系统错误,插件解析失败');
            }
        }
        $this->d_id = $this->_addons;
        $this->_action = $this->_action ?: 'index';
        $this->_controller = $this->_controller ? lcfirst($this->_controller) : 'index';
        parent::__construct();
    }

    public function initialize()
    {
        //判断插件状态
        $res = \app\common\facade\Addons::getAddonsByDId($this->d_id);
        if ($res['code'] != '1001') {
            if ($this->isAjax) {
                $this->results($res);
            } else {
                $this->error($res['msg'], '', '', '-1');
            }
        }
        if ($res['data']['status'] == 0) {
            if ($this->isAjax) {
                $this->results(['code' => 4001, 'msg' => '插件未启用', 'data' => '']);
            } else {
                $this->error('插件未启用', '', '', '-1');
            }
        }
        $this->getAddons();
        $this->getOptions();
    }

    public function getAddons()
    {
        if (is_null($this->addons)) {
            $class = cmf_get_addons_class($this->d_id);
            $this->addons = new $class;
        }
        return $this->addons;
    }

    public function getOptions()
    {
        $res = \app\common\facade\Addons::getAddonsOptions($this->d_id);
        if ($res['code'] == '1001') {
            $this->options = $res['data'];
        }
    }

    public function fetch($template = '', $vars = [], $config = [])
    {
        $new = [];
        $old = config('template.tpl_replace_string');
        $option = cmf_option('system');
        $path = $this->d_id . '/view/';
        if ($option['cdn_status']) {
            $new = [
                '__ADDONS_TMPL__' => $option['cdn_url'] . '/addons/' . $path,
                '__TMPL__' => $option['cdn_url'] . '/theme/admin',
                '__LAYUI__' => $option['cdn_url'] . $old['__LAYUI__'],
                '__STATIC__' => $option['cdn_url'] . $old['__STATIC__'],
            ];
        } else {
            $new['__ADDONS_TMPL__'] = '/addons/' . $this->name . '/view/';
            $new['__TMPL__'] = '/theme/admin';
        }
        $view_path = ADDONS_PATH . $path . $this->_controller . '/';;
        $this->view->config('view_path', $view_path);
        $this->view->config('tpl_replace_string', array_merge($old, $new));
        $template = $template ?: $this->_action;
        $template = $view_path . $template . '.html';
        return $this->view->fetch($template, $vars, $config);
    }
}